package com.sto.stoapp.db.manager;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.sto.stoapp.db.StoDataBase;
import com.sto.stoapp.entity.ServerSmsEntity;
import com.sto.stoapp.log.ExceptionLog;
import com.sto.stoapp.util.UnifiedStaticType;

public class ServerSmsManager extends StoDataBase {

	private static ServerSmsManager manager;

	// 地址
	private final String ADDRESS = "address";

	// 网点编号
	private final String CODE = "code";

	// 电话
	private final String PHONE = "phone";

	// 服务站点
	private final String SERVERSITE = "serversite";

	// 站点名称
	private final String SITE = "site";

	// 站点编号
	private final String SITECODE = "sitecode";

	public ServerSmsManager(Context context) {
		super(context);
	}

	/**
	 * 单例化对象
	 * 
	 * @param context
	 * @return
	 */
	public static ServerSmsManager getInstance(Context context) {
		synchronized (UnifiedStaticType.DATABASE_SYNCHRONIZED) {
			if (manager == null) {
				manager = new ServerSmsManager(context);
			}
			return manager;
		}
	}

	/**
	 * 删除该数据库表所有数据
	 * 
	 * @return
	 */
	public boolean deleteAll() {
		synchronized (UnifiedStaticType.DATABASE_SYNCHRONIZED) {
			long res = -1;
			try {
				res = getSqliteDatabase(UnifiedStaticType.WRITE_DATABASE)
						.delete(SERVER_SMS_DOWNLOAD_TABLE, null, null);
			} catch (Exception e) {
				e.printStackTrace();
				ExceptionLog.writeLog("deleteAll", e.getMessage());
			} finally {
				closeDb(UnifiedStaticType.WRITE_DATABASE);
			}
			return res > 0 ? true : false;
		}
	}

	private List<ServerSmsEntity> ResolveJsonData(String jsonString) {
		List<ServerSmsEntity> jsonList = new ArrayList<ServerSmsEntity>();
		if (jsonString.indexOf("[") != -1 && jsonString.indexOf("]") != -1) {
			// 添加json协议头，有利于解析
			jsonString = "{\"json\":" + jsonString + "}";
			System.out.println(jsonString);
			try {
				JSONObject jsonObj = new JSONObject(jsonString);
				JSONArray jsonArray = jsonObj.getJSONArray("json");

				for (int i = 0; i < jsonArray.length(); i++) {
					JSONObject dataObject = (JSONObject) jsonArray.get(i);
					ServerSmsEntity serverSms = new ServerSmsEntity();
					serverSms.setAddress(dataObject.getString(ADDRESS));
					serverSms.setNetCode(dataObject.getString(CODE));
					serverSms.setPhone(dataObject.getString(PHONE));
					serverSms.setServerSite(dataObject.getString(SERVERSITE));
					serverSms.setSiteCode(dataObject.getString(SITECODE));
					serverSms.setSiteName(dataObject.getString(SITE));
					jsonList.add(serverSms);
				}
			} catch (JSONException e) {
				e.printStackTrace();
				ExceptionLog.writeLog("ResolveJsonData", e.getMessage());
			}
		}

		return jsonList;
	}

	/**
	 * 批量更新数据
	 * 
	 * @param datas
	 */
	public int insertList(String jsonString) {
		int length = 0;

		if (jsonString.length() < 10)
			return length;
		List<ServerSmsEntity> jsonList = ResolveJsonData(jsonString);
		synchronized (UnifiedStaticType.DATABASE_SYNCHRONIZED) {
			deleteAll();// 清空列表
			SQLiteDatabase db = getSqliteDatabase(UnifiedStaticType.WRITE_DATABASE);
			if (null != jsonList) {
				try {
					db.beginTransaction();
					length = jsonList.size();
					for (int i = 0; i < length; i++) {
						long res = -1;

						ContentValues values = new ContentValues();
						values.put(SERVER_SMS_ADDRESS, jsonList.get(i)
								.getAddress());
						values.put(SERVER_SMS_CODE, jsonList.get(i)
								.getNetCode());
						values.put(SERVER_SMS_PHONE, jsonList.get(i).getPhone());
						values.put(SERVER_SMS_SERVERSITE, jsonList.get(i)
								.getServerSite());
						values.put(SERVER_SMS_SITE, jsonList.get(i)
								.getSiteName());
						values.put(SERVER_SMS_SITECODE, jsonList.get(i)
								.getSiteCode());
						res = db.insert(SERVER_SMS_DOWNLOAD_TABLE, null, values);
						if (res > 0) {
							Log.d("插入一条服务短信", "成功");
						} else {
							Log.d("插入一条服务短信", "失败");
						}
					}
				} catch (Exception e) {
					e.printStackTrace();
					ExceptionLog.writeLog("insertList", e.getMessage());
				} finally {
					jsonList.clear();
					db.setTransactionSuccessful();
					db.endTransaction();
					closeDb(UnifiedStaticType.WRITE_DATABASE);
				}
			}
		}
		return length;
	}
}
